package com.baiwei.vpnapi.service.impl;

import com.baiwei.vpnapi.dao.ChannelMapper;
import com.baiwei.vpnapi.dao.StatActionMapper;
import com.baiwei.vpnapi.dao.StatAdvertMapper;
import com.baiwei.vpnapi.dao.VpnAdvertMapper;
import com.baiwei.vpnapi.entity.*;
import com.baiwei.vpnapi.entity.reqentity.ChannelStatInput;
import com.baiwei.vpnapi.service.AdminService;
import com.baiwei.vpnapi.utils.AppUtils;
import com.baiwei.vpnapi.utils.JwtTokenUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;

/**
 * @program: vpnapi
 * @description: admin统计实现
 * @author: Mr. Stone
 * @create: 2020-03-13 21:00
 **/

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private StatActionMapper statActionMapper;

    @Autowired
    private StatAdvertMapper statAdvertMapper;

    @Autowired
    private VpnAdvertMapper vpnAdvertMapper;

    @Autowired
    private ChannelMapper channelMapper;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private JwtTokenUtil jwtTokenUtil;



    @Override
    public Object statAdvert(Integer actionType, ChannelStatInput input) {
        String token = request.getHeader("Authorization");
        String verson = request.getHeader("version");
        String platform = request.getHeader("platform");
        VpnUser vpnUser = jwtTokenUtil.getVpnUser(token);
        QueryWrapper<VpnAdvert> qw = new QueryWrapper<>();
        qw.lambda().eq(VpnAdvert::getAdId, input.getAdId());
        VpnAdvert advert = vpnAdvertMapper.selectOne(qw);
        if(advert == null) { return  null; }
        StatAdvertAction advertAction = new StatAdvertAction();
        advertAction.setAdId(advert.getAdId());
        advertAction.setUserId(vpnUser.getUserId());
        advertAction.setCreateTime(new Date());
        advertAction.setAppVersion(verson);
        advertAction.setPlatform(Integer.valueOf(platform));
        advertAction.setActionType(actionType);
        statAdvertMapper.insert(advertAction);
        return "success";
    }

    @Override
    public Object statAction(Integer actionType,
                             Integer userId,
                             String deviceIdentify,
                             Integer platform,
                             ChannelStatInput input){
        StatUserAction action = new StatUserAction();
        String ipString = AppUtils.getIpAddr(request);
        action.setIpAddr(ipString);
        action.setUserId(userId);
        action.setDeviceId(deviceIdentify);
        action.setActionType(actionType);
        action.setCreateTime(new Date());
        action.setPlatform(platform);
        HashMap<String, String> channelInfoMap =  AppUtils.platToChannel();
        String platformStr = platform.toString();
        String channelNoLocal = channelInfoMap.get(platformStr);
        // 添加渠道统计
        if(input != null) {
            String channelNo = input.getChannelNo();
            if(channelNo != null && channelNo.length() > 0) {
                ChannelInfo curChannel = channelMapper.getChannelByChannelNo(channelNo);
                if(curChannel != null) {
                    action.setChannelNo(channelNo);
                } else {
                    action.setChannelNo(channelNoLocal);
                }
            } else {
                action.setChannelNo(channelNoLocal);
            }
        } else {
            action.setChannelNo(channelNoLocal);
        }
        statActionMapper.insert(action);
        return "succcess";
    }
}
